package com.artPreter.service.impl;

import com.artPreter.mapper.DepartContentSectionMapper;
import com.artPreter.pojo.DepartContentSection;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.artPreter.mapper.TemplateDetailMapper;
import com.artPreter.pojo.Depart;
import com.artPreter.pojo.DepartSectionTemp;
import com.artPreter.mapper.DepartSectionTempMapper;
import com.artPreter.pojo.TemplateDetail;
import com.artPreter.service.IDepartContentSectionService;
import com.artPreter.service.IDepartSectionTempService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
 * <p>
 * 机构栏目表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-19
 */
@Service
public class DepartSectionTempServiceImpl extends ServiceImpl<DepartSectionTempMapper, DepartSectionTemp> implements IDepartSectionTempService {

    @Autowired
    private TemplateDetailMapper templateDetailMapper;
    @Autowired
    private IDepartContentSectionService departContentSectionService;
    @Autowired
    private DepartContentSectionMapper departContentSectionMapper;

    /**
     * 添加栏目
     * @param departSectionTemp
     */
    @Override
    public void add(DepartSectionTemp departSectionTemp) {
        String uuid = UUID.randomUUID().toString();
        if(departSectionTemp.getTempId()!=null){
            LambdaQueryWrapper<TemplateDetail> templateDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            templateDetailLambdaQueryWrapper.eq(TemplateDetail::getTempId, departSectionTemp.getTempId());
            List<TemplateDetail> templateDetailList = templateDetailMapper.selectList(templateDetailLambdaQueryWrapper);
            if(templateDetailList.size()>0){
                // 模板地址
                departSectionTemp.setTempAddr(templateDetailList.get(0).getTempAddr());
                // 内容类型
                departSectionTemp.setContentType(templateDetailList.get(0).getContentType());
                // 模板名称
                departSectionTemp.setTempName(templateDetailList.get(0).getName());

                LambdaQueryWrapper<DepartSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                if(departSectionTemp.getDepartId()!=null){
                    lambdaQueryWrapper.eq(DepartSectionTemp::getDepartId, departSectionTemp.getDepartId());
                }
                List<DepartSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
                if(list.size()==0){
                    departSectionTemp.setIsDefault(1);
                }
            }
        }
        departSectionTemp.setSectionId(uuid);
        this.baseMapper.insert(departSectionTemp);
        // 添加内容板块
        departContentSectionService.addContent(departSectionTemp);
    }

    /**
     * 批量保存栏目内容
     * @param departSectionTempList
     */
    @Override
    public void saveBatchData(List<DepartSectionTemp> departSectionTempList) {
        for (int i = 0; i < departSectionTempList.size(); i++) {
            // 首页导航
            if(departSectionTempList.get(i).getDepartId().equals("358a5bee-7600-4c88-803c-1c92e74b8dc7")){
                DepartContentSection departContentSection = new DepartContentSection();
                departContentSection.setSort(departSectionTempList.get(i).getSort());
                departContentSection.setName(departSectionTempList.get(i).getName());
                LambdaQueryWrapper<DepartContentSection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(DepartContentSection::getSectionId, departSectionTempList.get(i).getSectionId());
                departContentSectionMapper.update(departContentSection,lambdaQueryWrapper);
            }
            LambdaQueryWrapper<DepartSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DepartSectionTemp::getSectionId, departSectionTempList.get(i).getSectionId());
            this.baseMapper.update( departSectionTempList.get(i),lambdaQueryWrapper);
        }
    }

    @Override
    public void delete(DepartSectionTemp departSectionTemp) {

        this.baseMapper.deleteById(departSectionTemp);
        //如果该栏目是默认栏目，将其他栏目设置为默认栏目
        if(departSectionTemp.getIsDefault().equals(1)){
            LambdaQueryWrapper<DepartSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if(departSectionTemp.getDepartId()!=null){
                lambdaQueryWrapper.eq(DepartSectionTemp::getDepartId, departSectionTemp.getDepartId());
            }
            List<DepartSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
            if(list.get(0).getIsDefault().equals(1)){
                this.baseMapper.updateById(list.get(0));
            }
        }
        // 删除内容板块
        if(departSectionTemp.getSectionId()!=null&&!departSectionTemp.getSectionId().equals("")){
            LambdaQueryWrapper<DepartContentSection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DepartContentSection::getSectionId, departSectionTemp.getSectionId());
            departContentSectionMapper.delete(lambdaQueryWrapper);
        }

    }

    @Override
    public List<DepartSectionTemp> getList(DepartSectionTemp departSectionTemp) {
        List<DepartSectionTemp> list = new ArrayList<>();
        LambdaQueryWrapper<DepartSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(departSectionTemp.getDepartId()!=null){
            lambdaQueryWrapper.eq(DepartSectionTemp::getDepartId, departSectionTemp.getDepartId());
            lambdaQueryWrapper.orderByAsc(DepartSectionTemp::getSort);
            list = this.baseMapper.selectList(lambdaQueryWrapper);
        }
        return list;
    }
}
